define(["jquery","bbcare"],function($,$bb){
	var quest = {};

	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
    	obj.effectId = JSON.stringify(obj.id);
        obj.id = obj.id[0];
		return obj;
	}

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_staticAge" type="text/html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'   	<div class="control-input">'+
				'			<p class="form-control-static validatebox" name="{{id}}" data-effectId="{{effectId}}" data-type="{{type}}" data-validate="{{validate}}" data-attention="{{attention}}"></p>'+
				'  		</div>'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 处理value，得到年龄
	 */
	var handleVal = function($dom,values){
		var effectId = JSON.parse($dom.attr("data-effectId"));
		var value = {};
		if(values && values[effectId[0]]){
            var presentDate = $bb.simple.getCurTime().date;
            var bothDate = values[effectId[0]];
            var dueDate = values[effectId[1]] == "" ? bothDate : values[effectId[1]];
            var daysGestation = $bb.simple.getGestation(bothDate,dueDate).days;
            if (effectId.length == 3){
                if( values[effectId[2]] && values[effectId[2]] != "" ){
                    var arrGestation = JSON.parse(values[effectId[2]]);
                    daysGestation = arrGestation[0]*7 + arrGestation[1];
                }
            }
            var actualDays = $bb.simple.dateDiff(bothDate,presentDate).days;
            var correctDays = $bb.simple.dateDiff(dueDate,presentDate).days;
            var days;
            if(daysGestation < 37*7 && actualDays < 365*2){
                if(correctDays < 0){
                    days = correctDays + 40*7;
                    value.label = "纠正胎龄";
                }else{
                    days = correctDays;
                    value.label = "纠正年龄";
                }
            }else{
                days = actualDays;
                value.label = "年龄";
            }
            value.val = $bb.simple.formatAge(days);
        }
        return value;
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,values){
		var value = handleVal($dom,values);
		if( value.val ) $dom.html(value.val);
		if( value.label ) $dom.parent().prev().html(value.label);
	}

	return quest;
});